package com.yhq.spring.event.retry;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

/**
 * @author hqyin
 * @date 2023/3/13 3:04 下午
 */
@Component
@Slf4j
public class RetryEventListener {
   @EventListener
   @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 10_000L))
    public void handle(RetryEvent retryEvent) {
        log.info("receive add event：{}", retryEvent.getMsgId());
        throw new RuntimeException("xxxx");
    }


    /**
     * handle方法配置重试3次失败后会回调这个方法
     * @param e
     * @param retryEvent
     */
    @Recover
    public void recover(Exception e,RetryEvent retryEvent) {
        log.info("recover event: {}",retryEvent.getMsgId(),e);
    }
}
